library(cregg)
library(data.table)
library(Rmisc)
library(survey)

set.seed(1989)

# SET WORKING DIRECTORY TO THE LOCAL LOCATION OF THE REPLICATION ARCHIVE
setwd("")

# READING IN SAMPLING FRAME DATA
samplingframe_data <- fread("sampling_frame_comparison.csv", header = TRUE, stringsAsFactors = FALSE)

# READING IN CONJOINT RESPONSE DATA
hiring_data <- fread("hiring_data_all.csv", header = TRUE, stringsAsFactors = FALSE)

# READING IN FOLLOWUP RESPONSE DATA
followup_data <- fread("followup_survey_data.csv", header = TRUE, stringsAsFactors = FALSE)

################################################################################

# PAGE 10, IN-TEXT--MAIN SURVEY RESPONSE RATES

# HOW MANY PEOPLE WERE IN THE SAMPLING FRAME?
dim(samplingframe_data)

# HOW MANY PEOPLE RESPONDED TO THE SURVEY?
table(samplingframe_data$responded)

# WHAT WAS THE OVERALL RESPONSE RATE?
round((table(samplingframe_data$responded)[1]/dim(samplingframe_data)[1])*100, 1)

################################################################################

# PAGE 11, IN-TEXT--MAIN SURVEY DESCRIPTIVES

# SEE REPLICATION CODE FOR TABLE 4

################################################################################

# PAGE 12, IN-TEXT--MAIN SURVEY NUMBER OF OBSERVATIONS

# SEE REPLICATION CODE FOR TABLE A6

################################################################################

# PAGE 14, IN-TEXT--FOLLOW-UP SURVEY NUMBER OF OBSERVATIONS

# NOTE: SAME SAMPLING FRAME WAS USED, SO DENOMINATOR REMAINS THE SAME AND WE JUST
# USE THE NEW NUMERATOR
round((dim(followup_data)[1]/dim(samplingframe_data)[1])*100, 1)

################################################################################

# PAGES 15-18, IN-TEXT--DISCUSSION OF RESULTS FROM FOLLOWUP SURVEY

# SEE REPLICATION CODE FOR FIGURE A1

################################################################################

# PAGE 21--FIGURE 1

# ESTABLISHING BASELINE LEVELS FOR ALL ATTRIBUTE-LEVELS
hiring_data$app_gender <- factor(hiring_data$app_gender, levels = c("Male", "Female"))
hiring_data$app_community <- factor(hiring_data$app_community, levels = c("None listed", "Docent at local museum",
                                                                          "Volunteer at local food bank", "Youth sports coach"))
hiring_data$app_bilingual <- factor(hiring_data$app_bilingual, levels = c("0", "1"))
hiring_data$app_race <- factor(hiring_data$app_race, levels = c("White", "Black", "Asian", "Hispanic/Latino"))
hiring_data$app_lobby_length <- factor(hiring_data$app_lobby_length, levels = c("0 years", "<5 years", "5-10 years", ">10 years"))
hiring_data$app_lobby_policy_match <- factor(hiring_data$app_lobby_policy_match, levels = c("No Policy Match", "Policy Match"))
hiring_data$app_org_ideo_match <- factor(hiring_data$app_org_ideo_match, levels = c("Indeterminate", "Mismatch", "Match"))
hiring_data$app_prev_pol_exp_type <- factor(hiring_data$app_prev_pol_exp_type, levels = c("None", "Committee Staff", "Comms Director", "Leg Director", "Think Tank Director"))

hiring_data$pid3 <- ifelse(hiring_data$pid=="Strong Democrat", "Democrat", NA)
hiring_data$pid3 <- ifelse(hiring_data$pid=="Not a very strong Democrat", "Democrat", hiring_data$pid3)
hiring_data$pid3 <- ifelse(hiring_data$pid=="Lean Democrat", "Democrat", hiring_data$pid3)
hiring_data$pid3 <- ifelse(hiring_data$pid=="Strong Republican", "Republican", hiring_data$pid3)
hiring_data$pid3 <- ifelse(hiring_data$pid=="Not a very strong Republican", "Republican", hiring_data$pid3)
hiring_data$pid3 <- ifelse(hiring_data$pid=="Lean Republican", "Republican", hiring_data$pid3)
hiring_data$pid3 <- ifelse(hiring_data$pid=="Independent", "Independent", hiring_data$pid3)
hiring_data$pid3 <- as.factor(hiring_data$pid3)

results <- amce(data = hiring_data, choice ~ app_gender + app_community + app_race + app_bilingual + 
                  app_lobby_length*app_lobby_policy_match + app_org_ideo_match*app_prev_pol_exp_type,
                id = ~ ResponseId, alpha = 0.05/27)

pdf(file = "Fig1.pdf", family = "Times", height = 10, width=8)
par(mar=c(5.1,17,0.5,0.5))
plot(x=c(results$estimate[which(results$feature=="app_org_ideo_match" & results$level=="Mismatch")],
         results$estimate[which(results$feature=="app_org_ideo_match" & results$level=="Match")],
         results$estimate[which(results$feature=="app_org_ideo_match" & results$level=="Indeterminate")],
         results$estimate[which(results$feature=="app_prev_pol_exp_type" & results$level=="Committee Staff")],
         results$estimate[which(results$feature=="app_prev_pol_exp_type" & results$level=="Leg Director")],
         results$estimate[which(results$feature=="app_prev_pol_exp_type" & results$level=="Comms Director")],
         results$estimate[which(results$feature=="app_prev_pol_exp_type" & results$level=="Think Tank Director")],
         results$estimate[which(results$feature=="app_prev_pol_exp_type" & results$level=="None")],
         results$estimate[which(results$feature=="app_lobby_policy_match" & results$level=="Policy Match")],
         results$estimate[which(results$feature=="app_lobby_policy_match" & results$level=="No Policy Match")],
         results$estimate[which(results$feature=="app_lobby_length" & results$level==">10 years")],
         results$estimate[which(results$feature=="app_lobby_length" & results$level=="5-10 years")],
         results$estimate[which(results$feature=="app_lobby_length" & results$level=="<5 years")],
         results$estimate[which(results$feature=="app_lobby_length" & results$level=="0 years")]),
     y=c(1:3, 6:10, 13:14, 17:20),
     xlim=c(-.20,.60),
     xaxt="n",
     ylim=c(0.5, 21.5),
     pch=c(19, 1, 1, 19, 19, 19, 19, 1, 19, 1, 19, 1, 1, 1),
     tck=-.02,
     cex.axis=0.9,
     cex=1.25,
     ylab="",
     yaxt="n",
     xlab="",
     axes = FALSE,
     panel.first = c(abline(v=0,lwd=2, col="gray90",lty=2)))
segments(x0=c(results$lower[which(results$feature=="app_org_ideo_match" & results$level=="Mismatch")],
              results$lower[which(results$feature=="app_org_ideo_match" & results$level=="Match")],
              results$lower[which(results$feature=="app_prev_pol_exp_type" & results$level=="Committee Staff")],
              results$lower[which(results$feature=="app_prev_pol_exp_type" & results$level=="Leg Director")],
              results$lower[which(results$feature=="app_prev_pol_exp_type" & results$level=="Comms Director")],
              results$lower[which(results$feature=="app_prev_pol_exp_type" & results$level=="Think Tank Director")],
              results$lower[which(results$feature=="app_lobby_policy_match" & results$level=="Policy Match")],
              results$lower[which(results$feature=="app_lobby_length" & results$level==">10 years")],
              results$lower[which(results$feature=="app_lobby_length" & results$level=="5-10 years")],
              results$lower[which(results$feature=="app_lobby_length" & results$level=="<5 years")]),
         x1=c(results$upper[which(results$feature=="app_org_ideo_match" & results$level=="Mismatch")],
              results$upper[which(results$feature=="app_org_ideo_match" & results$level=="Match")],
              results$upper[which(results$feature=="app_prev_pol_exp_type" & results$level=="Committee Staff")],
              results$upper[which(results$feature=="app_prev_pol_exp_type" & results$level=="Leg Director")],
              results$upper[which(results$feature=="app_prev_pol_exp_type" & results$level=="Comms Director")],
              results$upper[which(results$feature=="app_prev_pol_exp_type" & results$level=="Think Tank Director")],
              results$upper[which(results$feature=="app_lobby_policy_match" & results$level=="Policy Match")],
              results$upper[which(results$feature=="app_lobby_length" & results$level==">10 years")],
              results$upper[which(results$feature=="app_lobby_length" & results$level=="5-10 years")],
              results$upper[which(results$feature=="app_lobby_length" & results$level=="<5 years")]),
         y0=c(1:2, 6:9, 13, 17:19))
axis(1,at=c(-.2, 0, .2, .4, .6), 
     labels = c("-20%", "0%", "20%", "40%", "60%"), cex.axis = 1.5)
par(mgp=c(0,11,0))
axis(2,at=c(1:3, 6:10, 13:14, 17:20),
     las=2,
     labels=c("Misaligned", "Aligned", "Indeterminate (baseline)",
              "Committee Staff", "Leg. Director",
              "Comm. Director", "Think Tank Director",
              "None (baseline)",
              "Policy Match", "No Policy Match (baseline)",
              "> 10 years", "5-10 years", "<5 years",
              "None (baseline)"),
     tck=0,
     lwd = 0,
     line = 3,
     cex.axis=1.5, hadj=0)
par(mgp=c(0,13,0))
axis(2,at=c(4, 11, 15, 21),
     las=2,
     labels=c(expression(~bold(~underline("Preference Alignment"))),
              expression(~bold(~underline("Type of Political Job"))),
              expression(~bold(~underline("Policy Experience"))),
              expression(~bold(~underline("Years of Lobbying Experience")))),
     tck=0,
     lwd = 0,
     line = 3,
     cex.axis=1.5, hadj=0)
mtext("Effect on selection", side = 1, at=0.2, line = 3, cex = 1.75)
text(c(results$estimate[which(results$feature=="app_org_ideo_match" & results$level=="Mismatch")],
       results$estimate[which(results$feature=="app_org_ideo_match" & results$level=="Match")],
       results$estimate[which(results$feature=="app_prev_pol_exp_type" & results$level=="Committee Staff")],
       results$estimate[which(results$feature=="app_prev_pol_exp_type" & results$level=="Leg Director")],
       results$estimate[which(results$feature=="app_prev_pol_exp_type" & results$level=="Comms Director")],
       results$estimate[which(results$feature=="app_prev_pol_exp_type" & results$level=="Think Tank Director")],
       results$estimate[which(results$feature=="app_lobby_policy_match" & results$level=="Policy Match")],
       results$estimate[which(results$feature=="app_lobby_length" & results$level==">10 years")],
       results$estimate[which(results$feature=="app_lobby_length" & results$level=="5-10 years")],
       results$estimate[which(results$feature=="app_lobby_length" & results$level=="<5 years")]),
     c(1:2, 6:9, 13, 17:19) + 0.4,
     paste0(sprintf("%.0f",round(c(results$estimate[which(results$feature=="app_org_ideo_match" & results$level=="Mismatch")],
                                   results$estimate[which(results$feature=="app_org_ideo_match" & results$level=="Match")],
                                   results$estimate[which(results$feature=="app_prev_pol_exp_type" & results$level=="Committee Staff")],
                                   results$estimate[which(results$feature=="app_prev_pol_exp_type" & results$level=="Leg Director")],
                                   results$estimate[which(results$feature=="app_prev_pol_exp_type" & results$level=="Comms Director")],
                                   results$estimate[which(results$feature=="app_prev_pol_exp_type" & results$level=="Think Tank Director")],
                                   results$estimate[which(results$feature=="app_lobby_policy_match" & results$level=="Policy Match")],
                                   results$estimate[which(results$feature=="app_lobby_length" & results$level==">10 years")],
                                   results$estimate[which(results$feature=="app_lobby_length" & results$level=="5-10 years")],
                                   results$estimate[which(results$feature=="app_lobby_length" & results$level=="<5 years")]
     )*100, 0)),"%"), cex=1.5)
dev.off()

################################################################################

# PAGE 26--FIGURE 2

# CREATING VARIABLES TO INDICATE WHETHER THE ORGANIZATION IS IDEOLOGICALLY
# ALIGNED WITH THE PARTY IN CONTROL OF CONGRESS
hiring_data$org_maj_min <- factor(hiring_data$org_maj_min, levels= c("bipartisan",
                                                                     "majority",
                                                                     "minority"))

# SUBSET TO RESPONSES FROM CONTEXTS WHERE THE APPLICANT HAS POLITICAL EXPERIENCE
# AND THE ORGANIZATION IS LIBERAL OR CONSERVATIVE SUCH THAT IT IS ALIGNED WITH
# EITHER THE MAJORITY OR MINORITY PARTY IN CONGRESS
hiring_data_alignment <- hiring_data[which(hiring_data$app_prev_pol_exp_type!="None" & hiring_data$org_maj_min!="bipartisan"),]
hiring_data_alignment$org_maj_min <- factor(hiring_data_alignment$org_maj_min, levels = c("minority", "majority"))

hiring_data_alignment$app_prev_pol_exp_type <- factor(hiring_data_alignment$app_prev_pol_exp_type, levels = c("Think Tank Director", "Committee Staff", "Comms Director", "Leg Director"))
hiring_data_alignment$app_org_ideo_match <- droplevels(hiring_data_alignment$app_org_ideo_match)
hiring_data_alignment$app_org_ideo_match <- factor(hiring_data_alignment$app_org_ideo_match, levels = c("Match", "Mismatch"))
hiring_data_alignment$org_maj_min <- droplevels(hiring_data_alignment$org_maj_min)

# ESTIMATING MODEL THAT CALCULATES ACIEs BY ORGANIZATION ALIGNMENT WITH CONGRESS
results_int <- cj(data = hiring_data_alignment, 
                  choice ~ app_gender + app_community + app_race + app_bilingual + 
                    app_lobby_length*app_lobby_policy_match + app_org_ideo_match + app_prev_pol_exp_type,
                  id = ~ ResponseId, by = ~org_maj_min, estimate = "amce", alpha = 0.05/27)

pdf(file = "Fig2.pdf", family = "Times", height = 8, width=8)
layout(matrix(c(1,2), ncol=1, byrow=TRUE),
       heights = c(0.80,0.20))
par(mar=c(5.1,17,0.5,0.5))
plot(x=c(results_int$estimate[which(results_int$feature=="app_org_ideo_match" & results_int$level=="Mismatch" & results_int$org_maj_min=="minority")],
         results_int$estimate[which(results_int$feature=="app_org_ideo_match" & results_int$level=="Mismatch" & results_int$org_maj_min=="majority")],
         results_int$estimate[which(results_int$feature=="app_org_ideo_match" & results_int$level=="Match" & results_int$org_maj_min=="minority")],
         results_int$estimate[which(results_int$feature=="app_org_ideo_match" & results_int$level=="Match" & results_int$org_maj_min=="majority")]
),
y=c(0.85, 1.15, 1.55, 1.95),
xlim=c(-.30,.20),
xaxt="n",
ylim=c(0.5, 2.5),
pch=c(2, 19, 2, 1),
tck=-.02,
cex.axis=0.9,
cex=1.25,
ylab="",
yaxt="n",
xlab="",
axes = FALSE,
panel.first = c(abline(v=0,lwd=2, col="gray90",lty=2)))
segments(x0=c(results_int$lower[which(results_int$feature=="app_org_ideo_match" & results_int$level=="Mismatch" & results_int$org_maj_min=="minority")],
              results_int$lower[which(results_int$feature=="app_org_ideo_match" & results_int$level=="Mismatch" & results_int$org_maj_min=="majority")]),
         x1=c(results_int$upper[which(results_int$feature=="app_org_ideo_match" & results_int$level=="Mismatch" & results_int$org_maj_min=="minority")],
              results_int$upper[which(results_int$feature=="app_org_ideo_match" & results_int$level=="Mismatch" & results_int$org_maj_min=="majority")]),
         y0=c(0.85, 1.15))
axis(1,at=c(-.3, -.2, -.1, 0, .1, .2), 
     labels = c("-30%", "-20%", "-10%", "0%", "10%", "20%"), cex.axis = 1.5)
par(mgp=c(0,11,0))
axis(2,at=c(1, 1.75),
     las=2,
     labels=c("Misaligned", "Aligned (baseline)"),
     tck=0,
     lwd = 0,
     line = 3,
     cex.axis=1.5, hadj=0)
par(mgp=c(0,13,0))
axis(2,at=c(2.2),
     las=2,
     labels=c(expression(~bold(~underline("Org. Ideological Alignment\nwith Applicant")))),
     tck=0,
     lwd = 0,
     line = 3,
     cex.axis=1.5, hadj=0)
mtext("Effect on selection", side = 1, at=-0.05, line = 3, cex = 1.75)
text(c(results_int$estimate[which(results_int$feature=="app_org_ideo_match" & results_int$level=="Mismatch" & results_int$org_maj_min=="minority")],
       results_int$estimate[which(results_int$feature=="app_org_ideo_match" & results_int$level=="Mismatch" & results_int$org_maj_min=="majority")]),
     c(0.85, 1.15) + 0.10,
     paste0(sprintf("%.0f",round(c(results_int$estimate[which(results_int$feature=="app_org_ideo_match" & results_int$level=="Mismatch" & results_int$org_maj_min=="minority")],
                                   results_int$estimate[which(results_int$feature=="app_org_ideo_match" & results_int$level=="Mismatch" & results_int$org_maj_min=="majority")]
     )*100, 0)),"%"), cex=1.5)
def_par <- par(no.readonly = T)
opar <- par(mar=c(0,0,0,0))
plot(0,0, type="n", axes=FALSE, xlab="", ylab="")
legend(-0.10,0.70,legend=c("Minority",
                           "Majority"),
       pch=c(17,19), col=c("black"), ncol = 2,
       title="Status of Party Aligned with Org.", cex=1.75)
par(def_par)
dev.off()

################################################################################

# PAGE 29--FIGURE 3

# SUBSETTING TO OBSERVATIONS WHERE THE JOB CANDIDATE HAD SOME LOBBYING EXPERIENCE,
# SOME POLITICAL EXPERIENCE, AND WAS NOT SITUATED IN A TASK WHERE THE HIRING
# ORGANIZATION WAS DESIGNATED AS BIPARTISAN

hiring_data_hth <- hiring_data[which(hiring_data$app_lobby_length!="0 years" & hiring_data$app_prev_pol_exp_type!="None" &
                                       hiring_data$org_ideo!="bipartisan"),]
hiring_data_hth$app_lobby_length <- factor(hiring_data_hth$app_lobby_length, levels = c("<5 years", "5-10 years", ">10 years"))
hiring_data_hth$app_prev_pol_exp_type <- factor(hiring_data_hth$app_prev_pol_exp_type, levels = c("Think Tank Director", 
                                                                                                  "Committee Staff", "Comms Director",
                                                                                                  "Leg Director"))
hiring_data_hth$app_org_ideo_match <- droplevels(hiring_data_hth$app_org_ideo_match)
hiring_data_hth$app_org_ideo_match <- factor(hiring_data_hth$app_org_ideo_match, levels = c("Match", "Mismatch"))

results_hth <- amce(data = hiring_data_hth, choice ~ app_gender + app_community + app_race + app_bilingual + 
                      app_lobby_length*app_lobby_policy_match + app_org_ideo_match*app_prev_pol_exp_type,
                    id = ~ ResponseId, alpha = 0.05/27)

pdf(file = "Fig3.pdf", family = "Times", height = 8, width=8)
par(mar=c(5.1,17,0.5,0.5))
plot(x=c(results_hth$estimate[which(results_hth$feature=="app_org_ideo_match" & results_hth$level=="Mismatch")],
         results_hth$estimate[which(results_hth$feature=="app_org_ideo_match" & results_hth$level=="Match")],
         results_hth$estimate[which(results_hth$feature=="app_prev_pol_exp_type" & results_hth$level=="Committee Staff")],
         results_hth$estimate[which(results_hth$feature=="app_prev_pol_exp_type" & results_hth$level=="Leg Director")],
         results_hth$estimate[which(results_hth$feature=="app_prev_pol_exp_type" & results_hth$level=="Comms Director")],
         results_hth$estimate[which(results_hth$feature=="app_prev_pol_exp_type" & results_hth$level=="Think Tank Director")],
         results_hth$estimate[which(results_hth$feature=="app_lobby_policy_match" & results_hth$level=="Policy Match")],
         results_hth$estimate[which(results_hth$feature=="app_lobby_policy_match" & results_hth$level=="No Policy Match")],
         results_hth$estimate[which(results_hth$feature=="app_lobby_length" & results_hth$level==">10 years")],
         results_hth$estimate[which(results_hth$feature=="app_lobby_length" & results_hth$level=="5-10 years")],
         results_hth$estimate[which(results_hth$feature=="app_lobby_length" & results_hth$level=="<5 years")]),
     y=c(1:2, 5:8, 11:12, 15:17),
     xlim=c(-.20,.60),
     xaxt="n",
     ylim=c(0.5, 18.5),
     pch=c(1, 1, 19, 19, 1, 1, 19, 1, 19, 1, 1),
     tck=-.02,
     cex.axis=0.9,
     cex=1.25,
     ylab="",
     yaxt="n",
     xlab="",
     axes = FALSE,
     panel.first = c(abline(v=0,lwd=2, col="gray90",lty=2)))
segments(x0=c(results_hth$lower[which(results_hth$feature=="app_org_ideo_match" & results_hth$level=="Mismatch")],
              results_hth$lower[which(results_hth$feature=="app_prev_pol_exp_type" & results_hth$level=="Committee Staff")],
              results_hth$lower[which(results_hth$feature=="app_prev_pol_exp_type" & results_hth$level=="Leg Director")],
              results_hth$lower[which(results_hth$feature=="app_prev_pol_exp_type" & results_hth$level=="Comms Director")],
              results_hth$lower[which(results_hth$feature=="app_lobby_policy_match" & results_hth$level=="Policy Match")],
              results_hth$lower[which(results_hth$feature=="app_lobby_length" & results_hth$level==">10 years")],
              results_hth$lower[which(results_hth$feature=="app_lobby_length" & results_hth$level=="5-10 years")]),
         x1=c(results_hth$upper[which(results_hth$feature=="app_org_ideo_match" & results_hth$level=="Mismatch")],
              results_hth$upper[which(results_hth$feature=="app_prev_pol_exp_type" & results_hth$level=="Committee Staff")],
              results_hth$upper[which(results_hth$feature=="app_prev_pol_exp_type" & results_hth$level=="Leg Director")],
              results_hth$upper[which(results_hth$feature=="app_prev_pol_exp_type" & results_hth$level=="Comms Director")],
              results_hth$upper[which(results_hth$feature=="app_lobby_policy_match" & results_hth$level=="Policy Match")],
              results_hth$upper[which(results_hth$feature=="app_lobby_length" & results_hth$level==">10 years")],
              results_hth$upper[which(results_hth$feature=="app_lobby_length" & results_hth$level=="5-10 years")]),
         y0=c(1, 5:7, 11, 15:16))
axis(1,at=c(-.2, 0, .2, .4, .6), 
     labels = c("-20%", "0%", "20%", "40%", "60%"), cex.axis = 1.5)
par(mgp=c(0,11,0))
axis(2,at=c(1:2, 5:8, 11:12, 15:17),
     las=2,
     labels=c("Misaligned", "Aligned (baseline)",
              "Committee Staff", "Leg. Director",
              "Comm. Director", "Think Tank Director (baseline)",
              "Policy Match", "No Policy Match (baseline)",
              "> 10 years", "5-10 years", "<5 years (baseline)"),
     tck=0,
     lwd = 0,
     line = 3,
     cex.axis=1.5, hadj=0)
par(mgp=c(0,13,0))
axis(2,at=c(3, 9, 13, 18),
     las=2,
     labels=c(expression(~bold(~underline("Ideological Alignment"))),
              expression(~bold(~underline("Political Experience"))), 
              expression(~bold(~underline("Policy Experience"))), 
              expression(~bold(~underline("Lobbying Experience")))),
     tck=0,
     lwd = 0,
     line = 3,
     cex.axis=1.5, hadj=0)
mtext("Effect on selection", side = 1, at=0.2, line = 3, cex = 1.75)
text(c(results_hth$estimate[which(results_hth$feature=="app_org_ideo_match" & results_hth$level=="Mismatch")],
       results_hth$estimate[which(results_hth$feature=="app_prev_pol_exp_type" & results_hth$level=="Committee Staff")],
       results_hth$estimate[which(results_hth$feature=="app_prev_pol_exp_type" & results_hth$level=="Leg Director")],
       results_hth$estimate[which(results_hth$feature=="app_prev_pol_exp_type" & results_hth$level=="Comms Director")],
       results_hth$estimate[which(results_hth$feature=="app_lobby_policy_match" & results_hth$level=="Policy Match")],
       results_hth$estimate[which(results_hth$feature=="app_lobby_length" & results_hth$level==">10 years")],
       results_hth$estimate[which(results_hth$feature=="app_lobby_length" & results_hth$level=="5-10 years")]),
     c(1, 5:7, 11, 15:16) + 0.4,
     paste0(sprintf("%.0f",round(c(results_hth$estimate[which(results_hth$feature=="app_org_ideo_match" & results_hth$level=="Mismatch")],
                                   results_hth$estimate[which(results_hth$feature=="app_prev_pol_exp_type" & results_hth$level=="Committee Staff")],
                                   results_hth$estimate[which(results_hth$feature=="app_prev_pol_exp_type" & results_hth$level=="Leg Director")],
                                   results_hth$estimate[which(results_hth$feature=="app_prev_pol_exp_type" & results_hth$level=="Comms Director")],
                                   results_hth$estimate[which(results_hth$feature=="app_lobby_policy_match" & results_hth$level=="Policy Match")],
                                   results_hth$estimate[which(results_hth$feature=="app_lobby_length" & results_hth$level==">10 years")],
                                   results_hth$estimate[which(results_hth$feature=="app_lobby_length" & results_hth$level=="5-10 years")]
     )*100, 0)),"%"), cex=1.5)
dev.off()

################################################################################

# PAGE APPENDIX 13, IN-TEXT--RESPONSE RATES OF MAIN SURVEY

# SEE REPLICATION CODE FOR PAGE 10 OF MAIN PAPER

################################################################################

# PAGES APPENDIX 14-17--TABLES A3 AND A4, RELATED IN-TEXT DISCUSSION

# TABLE A3 AND TESTS OF WHETHER DIFFERENCES IN CHARACTERISTICS OF SAMPLING FRAME
# AND RESPODNENTS ARE DISTINGUISHABLE

# EMPLOYER TYPE
table(samplingframe_data$Isfirm, useNA = "always")
round(prop.table(table(samplingframe_data$Isfirm, useNA = "always")), 3)*100
table(samplingframe_data$Isfirm[which(samplingframe_data$responded==1)], useNA = "always")
round(prop.table(table(samplingframe_data$Isfirm[which(samplingframe_data$responded==1)], useNA = "always")), 3)*100
# ARE DIFFERENCES DISTINGUISHABLE?
prop.test(table(samplingframe_data$Isfirm,
                is.na(samplingframe_data$responded)))
t.test(samplingframe_data$Isfirm[which(samplingframe_data$responded==1)]=="y",
       samplingframe_data$Isfirm[is.na(samplingframe_data$responded)]=="y")

# LOBBYING EXPENDITURES
# $ AMOUNTS ARE ONLY REPORTED IF IN EXCESS OF $5,000, $0 OTHERWISE; WE FIRST 
# BRING ALL NON-REPORTED AMOUNTS TO $5,000, THEN BREAK INTO QUARTILES
samplingframe_data$Amount <- as.numeric(ifelse(samplingframe_data$Amount==0, 5000, samplingframe_data$Amount))
samplingframe_data$AmountQuart <- dplyr::ntile(samplingframe_data$Amount, 4)

table(samplingframe_data$AmountQuart, useNA = "always")
round(prop.table(table(samplingframe_data$AmountQuart, useNA = "always")), 3)*100
table(samplingframe_data$AmountQuart[which(samplingframe_data$responded==1)], useNA = "always")
round(prop.table(table(samplingframe_data$AmountQuart[which(samplingframe_data$responded==1)], useNA = "always")), 3)*100
# ARE DIFFERENCES DISTINGUISHABLE?
chisq.test(table(samplingframe_data$AmountQuart, !is.na(samplingframe_data$responded)))
t.test(samplingframe_data$Amount[which(samplingframe_data$responded==1)],
       samplingframe_data$Amount[is.na(samplingframe_data$responded)])

# REGISTERED LOBBYIST
table(samplingframe_data$registered_lobbyist, useNA = "always")
round(prop.table(table(samplingframe_data$registered_lobbyist, useNA = "always")), 3)*100
table(samplingframe_data$registered_lobbyist[which(samplingframe_data$responded==1)], useNA = "always")
round(prop.table(table(samplingframe_data$registered_lobbyist[which(samplingframe_data$responded==1)], useNA = "always")), 3)*100
# ARE DIFFERENCES DISTINGUISHABLE?
prop.test(table(samplingframe_data$registered_lobbyist,
                is.na(samplingframe_data$responded)))
t.test(samplingframe_data$registered_lobbyist[which(samplingframe_data$responded==1)],
       samplingframe_data$registered_lobbyist[is.na(samplingframe_data$responded)])

# CRP CATEGORY CODING
# SECTOR CODINGS ARE BASED ON THE FIRST LETTER IN THE CODE, SO WE ISOLATE THAT
# LETTER AND RECODE ACCORDINGLY

samplingframe_data$cat_letter <- toupper(substr(samplingframe_data$Catcode, 1, 1))

CRPcatRecode <- function(x){
  tmp <- car::recode(x,"'A'='Agribusiness';
                    'B'='Construction';
                    'C'='Communications and Electronics';
                    'D'='Defense';
                    'E'='Energy and Natural Resources';
                    'F'='Finance, Insurance and Real Estate';
                    'G'='Misc Business';
                    'H'='Health';
                    'J'='Ideological and Single-Issue';
                    'K'='Lawyers and Lobbyists';
                    'L'='Labor';
                    'M'='Misc Business';
                    'T'='Transportation';
                    'X'='Other';
                    'Y'='Unknown';
                    'Z'='Unknown';
                    else=NA")
  return(as.character(tmp))
}

samplingframe_data$CRPcat <- CRPcatRecode(samplingframe_data$cat_letter)

table(samplingframe_data$CRPcat, useNA = "always")
round(prop.table(table(samplingframe_data$CRPcat, useNA = "always")), 3)*100
table(samplingframe_data$CRPcat[which(samplingframe_data$responded==1)], useNA = "always")
round(prop.table(table(samplingframe_data$CRPcat[which(samplingframe_data$responded==1)], useNA = "always")), 3)*100
# ARE DIFFERENCES DISTINGUISHABLE?
chisq.test(table(samplingframe_data$CRPcat, !is.na(samplingframe_data$responded)))

# TABLE A4

# FULL DATA SET HAS ONE OBSERVATION FOR EACH PROFILE EVALUATED BY
# EACH RESPONDENT; TO GET DOWN TO ONE OBSERVATION PER RESPONDENT, WE ISOLATE
# ONLY THE DEMOGRAPHIC CHARACTERISTICS AND THEN DEDUP
respondent_data <- hiring_data[which(!is.na(hiring_data$choice)),1:19]
respondent_data <- respondent_data[!duplicated(respondent_data)]

# GENDER
table(respondent_data$gender, useNA = "always")
round(prop.table(table(respondent_data$gender, useNA = "always")),3)*100

# AGE
table(respondent_data$age, useNA = "always")
round(prop.table(table(respondent_data$age, useNA = "always")),3)*100

# EDUCATION
table(respondent_data$educ, useNA = "always")
round(prop.table(table(respondent_data$educ, useNA = "always")),3)*100

# RACE
table(respondent_data$race, useNA = "always")
round(prop.table(table(respondent_data$race, useNA = "always")),3)*100

# ETHNICITY
table(respondent_data$hispanic, useNA = "always")
round(prop.table(table(respondent_data$hispanic, useNA = "always")),3)*100

# INCOME
table(respondent_data$income, useNA = "always")
round(prop.table(table(respondent_data$income, useNA = "always")),3)*100

# IDEOLOGY
table(respondent_data$resp_ideo, useNA = "always")
round(prop.table(table(respondent_data$resp_ideo, useNA = "always")),3)*100

# PID
table(respondent_data$pid, useNA = "always")
round(prop.table(table(respondent_data$pid, useNA = "always")),3)*100

# LOBBYING EXPERIENCE
table(respondent_data$years_exp, useNA = "always")
round(prop.table(table(respondent_data$years_exp, useNA = "always")),3)*100

### PREVIOUS GOVERNMENT EXPERIENCE--RESPONDENTS COULD SELECT MORE THAN ONE OF 
### THE FOLLOWING
# MEMBER OF CONGRESS
table(respondent_data$mem_congress, useNA = "always")
round(prop.table(table(respondent_data$mem_congress, useNA = "always")),3)*100

# CONGRESSIONAL STAFF
table(respondent_data$cong_staff, useNA = "always")
round(prop.table(table(respondent_data$cong_staff, useNA = "always")),3)*100

# PRESIDENTIAL APPOINTEE
table(respondent_data$pres_appt, useNA = "always")
round(prop.table(table(respondent_data$pres_appt, useNA = "always")),3)*100

# EOP STAFFER
table(respondent_data$wh_staff, useNA = "always")
round(prop.table(table(respondent_data$wh_staff, useNA = "always")),3)*100

# CIVIL SERVANT
table(respondent_data$civil_servant, useNA = "always")
round(prop.table(table(respondent_data$civil_servant, useNA = "always")),3)*100

# OTHER
table(respondent_data$other_exp, useNA = "always")
round(prop.table(table(respondent_data$other_exp, useNA = "always")),3)*100

# NONE
table(respondent_data$any_exp, useNA = "always")
round(prop.table(table(respondent_data$any_exp, useNA = "always")),3)*100
###

# CURRENT ROLE WITH CLIENT
table(respondent_data$position, useNA = "always")
round(prop.table(table(respondent_data$position, useNA = "always")),3)*100

# TYPICAL INVOLVEMENT IN HIRING PROCESS WITH CURRENT EMPLOYER
table(respondent_data$hiring_exp, useNA = "always")
round(prop.table(table(respondent_data$hiring_exp, useNA = "always")),3)*100

################################################################################

# PAGE APPENDIX 14, IN-TEXT--WEIGHTED VERSION OF MAIN ANALYSIS

# MERGE SAMPLING FRAME DEMOGRAPHICS WITH CONJOINT RESPONSES
weighting_data <- merge(hiring_data, samplingframe_data, by = "ResponseId", all.x = TRUE)

# DROP ANY OBSERVATIONS WITH MISSING OUTCOMES
weighting_data <- weighting_data[which(!is.na(weighting_data$choice)),]

# creating survey weights using distributions of covariates available for full sample
# (lobbyist employer, expenditures, CRP category, registered or not)

# CREATING SURVEY WEIGHTS USING THE DISTRIBUTIONS OF THE FOUR COVARIATES SOURCED
# FROM CRP AS IN THE FULL SAMPLE
final_list_weights <- svydesign(ids=~1, data = weighting_data[!duplicated(weighting_data$ResponseId),])

firm.dist <- data.frame(Isfirm = c("", "y"),
                        Freq = nrow(final_list_weights) * c(0.560, 0.440))

reglob.dist <- data.frame(registered_lobbyist = c("0", "1"),
                          Freq = nrow(final_list_weights) * c(0.247, 0.753))

spend.dist <- data.frame(AmountQuart = c("1", "2", "3", "4"),
                         Freq = nrow(final_list_weights) * c(0.250, 0.250, 0.250, 0.250))

cat.dist <- data.frame(CRPcat = c("Agribusiness", "Communications and Electronics",
                                  "Construction", "Defense", "Energy and Natural Resources",
                                  "Finance, Insurance and Real Estate", 
                                  "Health", "Ideological and Single-Issue",
                                  "Labor", "Lawyers and Lobbyists",
                                  "Misc Business", "Other", "Transportation",
                                  "Unknown"),
                       Freq = nrow(final_list_weights) * c(0.041, 0.075, 0.020, 0.018,
                                                           0.071, 0.106, 0.196, 0.010,
                                                           0.022, 0.06, 0.127, 0.057,
                                                           0.071, 0.090))

final_list_weights.rake <- rake(design = final_list_weights,
                                sample.margins = list(~Isfirm, ~registered_lobbyist,
                                                      ~AmountQuart, ~CRPcat),
                                population.margins = list(firm.dist, reglob.dist,
                                                          spend.dist, cat.dist))

survey_weights <- data.frame("ResponseId"=final_list_weights.rake$variables$ResponseId, 
                             "weight"=final_list_weights.rake$prob)

weighting_data <- merge(weighting_data, survey_weights, by = "ResponseId", all.x = TRUE)

results_weighted <- amce(data = weighting_data, choice ~ app_gender + app_community + app_race + app_bilingual + 
                           app_lobby_length*app_lobby_policy_match + app_org_ideo_match*app_prev_pol_exp_type,
                         id = ~ ResponseId, weights = weighting_data$weight, alpha = 0.05/27)
results_weighted

################################################################################

# PAGE APPENDIX 20--FIGURE A1

# CALCULATING MEANS AND 95% CIS FOR RESPONDENTS' PERCEPTIONS OF HOW MUCH OF EACH
# TRAIT

connections_thinktank <- CI(followup_data$Connections_1[!is.na(followup_data$Connections_1)], ci=0.95)
connections_comms <- CI(followup_data$Connections_2[!is.na(followup_data$Connections_2)], ci=0.95)
connections_leg <- CI(followup_data$Connections_3[!is.na(followup_data$Connections_3)], ci=0.95)
connections_committee <- CI(followup_data$Connections_4[!is.na(followup_data$Connections_4)], ci=0.95)

proc_expertise_thinktank <- CI(followup_data$ProceduralExpertise_1[!is.na(followup_data$ProceduralExpertise_1)], ci=0.95)
proc_expertise_comms <- CI(followup_data$ProceduralExpertise_2[!is.na(followup_data$ProceduralExpertise_2)], ci=0.95)
proc_expertise_leg <- CI(followup_data$ProceduralExpertise_3[!is.na(followup_data$ProceduralExpertise_3)], ci=0.95)
proc_expertise_committee <- CI(followup_data$ProceduralExpertise_4[!is.na(followup_data$ProceduralExpertise_4)], ci=0.95)

tax_thinktank <- CI(followup_data$TaxPolicy_1[!is.na(followup_data$TaxPolicy_1)], ci=0.95)
tax_comms <- CI(followup_data$TaxPolicy_2[!is.na(followup_data$TaxPolicy_2)], ci=0.95)
tax_leg <- CI(followup_data$TaxPolicy_3[!is.na(followup_data$TaxPolicy_3)], ci=0.95)
tax_committee <- CI(followup_data$TaxPolicy_4[!is.na(followup_data$TaxPolicy_4)], ci=0.95)

realestate_thinktank <- CI(followup_data$RealEstatePolicy_1[!is.na(followup_data$RealEstatePolicy_1)], ci=0.95)
realestate_comms <- CI(followup_data$RealEstatePolicy_2[!is.na(followup_data$RealEstatePolicy_2)], ci=0.95)
realestate_leg <- CI(followup_data$RealEstatePolicy_3[!is.na(followup_data$RealEstatePolicy_3)], ci=0.95)
realestate_committee <- CI(followup_data$RealEstatePolicy_4[!is.na(followup_data$RealEstatePolicy_4)], ci=0.95)

pdf(file = "FigA1.pdf", family = "Times", height = 10, width = 12)
layout(matrix(c(1,2), ncol=1, byrow=TRUE),
       heights = c(0.80,0.20))
par(mar=c(5.1,17,0.5,0.5))
plot(x=c(tax_committee[2],
         tax_leg[2],
         tax_comms[2],
         tax_thinktank[2],
         realestate_committee[2],
         realestate_leg[2],
         realestate_comms[2],
         realestate_thinktank[2],
         proc_expertise_committee[2],
         proc_expertise_leg[2],
         proc_expertise_comms[2],
         proc_expertise_thinktank[2],
         connections_committee[2],
         connections_leg[2],
         connections_comms[2],
         connections_thinktank[2]),
     y=c(1:4, 7:10, 13:16, 19:22),
     xlim=c(0,10),
     xaxt="n",
     ylim=c(0.5, 22.5),
     pch=rev(21:24),
     bg="black",
     tck=-.02,
     cex.axis=0.9,
     cex=1.25,
     ylab="",
     yaxt="n",
     xlab="",
     axes = FALSE)
segments(x0=c(tax_committee[3],
              tax_leg[3],
              tax_comms[3],
              tax_thinktank[3],
              realestate_committee[3],
              realestate_leg[3],
              realestate_comms[3],
              realestate_thinktank[3],
              proc_expertise_committee[3],
              proc_expertise_leg[3],
              proc_expertise_comms[3],
              proc_expertise_thinktank[3],
              connections_committee[3],
              connections_leg[3],
              connections_comms[3],
              connections_thinktank[3]),
         x1=c(tax_committee[1],
              tax_leg[1],
              tax_comms[1],
              tax_thinktank[1],
              realestate_committee[1],
              realestate_leg[1],
              realestate_comms[1],
              realestate_thinktank[1],
              proc_expertise_committee[1],
              proc_expertise_leg[1],
              proc_expertise_comms[1],
              proc_expertise_thinktank[1],
              connections_committee[1],
              connections_leg[1],
              connections_comms[1],
              connections_thinktank[1]),
         y0=c(1:4, 7:10, 13:16, 19:22))
axis(1,at=0:10, 
     labels = 0:10, cex.axis = 1.5)
par(mgp=c(0,11,0))
axis(2,at=c(2.5, 8.5, 14.5, 20.5),
     las=2,
     labels=c("Tax Policy Expertise",
              "Real Estate Policy Expertise",
              "Procedural Expertise",
              "Connections"),
     tck=0,
     lwd = 0,
     line = 3,
     cex.axis=1.5, hadj=0)
mtext("Perception of Trait", side = 1, at=5.5, line = 3, cex = 1.75)
text(c(tax_committee[2],
       tax_leg[2],
       tax_comms[2],
       tax_thinktank[2],
       realestate_committee[2],
       realestate_leg[2],
       realestate_comms[2],
       realestate_thinktank[2],
       proc_expertise_committee[2],
       proc_expertise_leg[2],
       proc_expertise_comms[2],
       proc_expertise_thinktank[2],
       connections_committee[2],
       connections_leg[2],
       connections_comms[2],
       connections_thinktank[2]),
     c(1:4, 7:10, 13:16, 19:22) + 0.6,
     round(c(tax_committee[2],
             tax_leg[2],
             tax_comms[2],
             tax_thinktank[2],
             realestate_committee[2],
             realestate_leg[2],
             realestate_comms[2],
             realestate_thinktank[2],
             proc_expertise_committee[2],
             proc_expertise_leg[2],
             proc_expertise_comms[2],
             proc_expertise_thinktank[2],
             connections_committee[2],
             connections_leg[2],
             connections_comms[2],
             connections_thinktank[2]),
           2), cex=1.5)
def_par <- par(no.readonly = T)
opar <- par(mar=c(0,0,0,0))
plot(0,0, type="n", axes=FALSE, xlab="", ylab="")
legend(-0.35,0.70,legend=c("Think Tank Director",
                           "Communications Director",
                           "Legislative Director",
                           "Committee Staff"),
       pch=21:25, pt.bg = "black", ncol = 2,
       title="Type of Political Experience", cex=1.75)
par(def_par)
dev.off()

################################################################################

# PAGE APPENDIX 29--TABLE A6

# RESULTS FORMATTED FOR INPUT TO TABLE
# PLEASE NOTE THAT THE round() FUNCTION ROUNDS NEGATIVE VALUES NEAR ZERO TO 0.00
# INSTEAD OF -0.00, WHICH LEADS TO A FEW INCONSISTENCIES BETWEEN THE OUTPUT
# OF THIS LINE OF CODE VS. WHAT IS IN THE ONLINE APPENDIX (ANY 0.00 VALUES
# WERE MANUALLY CHECKED BY THE AUTHORS TO DETERMINE IF THEY ARE POSITIVE OR
# NEGATIVE)
cbind(as.character(results$feature), as.character(results$level), 
      round(results$estimate, 2), round(results$std.error,2),
      round(results$lower, 2), round(results$upper,2))
# NUMBER OF RESPONDENTS
length(unique(hiring_data$ResponseId[which(!is.na(hiring_data$choice))]))
# NUMBER OF PROFILE EVALUATIONS
sum(!is.na(hiring_data$choice))

# amce FROM cregg USES THE survey PACKAGE TO ESTIMATE THE UNDERLYING LINEAR
# REGRESSION, BUT DOES NOT PROVIDE ANY GOODNESS OF FIT STATISTICS FROM THAT
# REGRESSION MODEL; TO OBTAIN THOSE STATISTICS, WE RE-ESTIMATE THE svyglm
# MODEL

survey_design <- svydesign(ids = ~ResponseId, weights = ~1, data = hiring_data)
reg_model <- svyglm(choice ~ app_gender + app_community + app_race + app_bilingual + 
                      app_lobby_length*app_lobby_policy_match + 
                      app_org_ideo_match*app_prev_pol_exp_type,
                    design = survey_design)

# THE RESULTING svyglm OBJECT INCLUDES AN AIC ATTRIBUTE
reg_model$aic

################################################################################

# PAGE APPENDIX 31--TABLE A7

results_rating <- amce(data = hiring_data, rating ~ app_gender + app_community + app_race + app_bilingual + 
                  app_lobby_length*app_lobby_policy_match + app_org_ideo_match*app_prev_pol_exp_type,
                id = ~ ResponseId, alpha = 0.05/27)
# results_rating FORMATTED FOR INPUT TO TABLE
cbind(as.character(results_rating$feature), as.character(results_rating$level), 
      round(results_rating$estimate, 2), round(results_rating$std.error,2),
      round(results_rating$lower, 2), round(results_rating$upper,2))
# NUMBER OF RESPONDENTS
length(unique(hiring_data$ResponseId[which(!is.na(hiring_data$rating))]))
# NUMBER OF PROFILE EVALUATIONS
sum(!is.na(hiring_data$rating))

# amce FROM cregg USES THE survey PACKAGE TO ESTIMATE THE UNDERLYING LINEAR
# REGRESSION, BUT DOES NOT PROVIDE ANY GOODNESS OF FIT STATISTICS FROM THAT
# REGRESSION MODEL; TO OBTAIN THOSE STATISTICS, WE RE-ESTIMATE THE svyglm
# MODEL

survey_design <- svydesign(ids = ~ResponseId, weights = ~1, data = hiring_data)
reg_model <- svyglm(rating ~ app_gender + app_community + app_race + app_bilingual + 
                      app_lobby_length*app_lobby_policy_match + app_org_ideo_match*app_prev_pol_exp_type,
                    design = survey_design)

# THE RESULTING svyglm OBJECT INCLUDES AN AIC ATTRIBUTE
reg_model$aic

################################################################################

# PAGE APPENDIX 33--TABLE A8

# results_hth FORMATTED FOR INPUT TO TABLE
cbind(as.character(results_hth$feature), as.character(results_hth$level), round(results_hth$estimate, 2), 
      round(results_hth$std.error,2), round(results_hth$lower, 2), round(results_hth$upper,2))
# NUMBER OF RESPONDENTS
length(unique(hiring_data_hth$ResponseId[which(!is.na(hiring_data_hth$choice))]))
# NUMBER OF PROFILE EVALUATIONS
sum(!is.na(hiring_data_hth$choice))

# amce FROM cregg USES THE survey PACKAGE TO ESTIMATE THE UNDERLYING LINEAR
# REGRESSION, BUT DOES NOT PROVIDE ANY GOODNESS OF FIT STATISTICS FROM THAT
# REGRESSION MODEL; TO OBTAIN THOSE STATISTICS, WE RE-ESTIMATE THE svyglm
# MODEL

survey_design <- svydesign(ids = ~ResponseId, weights = ~1, data = hiring_data_hth)
reg_model <- svyglm(choice ~ app_gender + app_community + app_race + app_bilingual + 
                      app_lobby_length*app_lobby_policy_match + 
                      app_org_ideo_match*app_prev_pol_exp_type,
                    design = survey_design)

# THE RESULTING svyglm OBJECT INCLUDES AN AIC ATTRIBUTE
reg_model$aic

################################################################################

# PAGE APPENDIX 35--TABLE A9

# results_int FORMATTED FOR INPUT TO TABLE
cbind(as.character(results_int$feature[1:24]), as.character(results_int$level[1:24]), 
      round(results_int$estimate[1:24], 2), 
      round(results_int$std.error[1:24],2), round(results_int$lower[1:24], 2), 
      round(results_int$upper[1:24],2),
      round(results_int$estimate[25:48], 2), 
      round(results_int$std.error[25:48],2), round(results_int$lower[25:48], 2), 
      round(results_int$upper[25:48],2))
# NUMBER OF RESPONDENTS
length(unique(hiring_data_alignment$ResponseId[which(!is.na(hiring_data_alignment$choice))]))
# NUMBER OF PROFILE EVALUATIONS
sum(!is.na(hiring_data_alignment$choice))

# cj FROM cregg USES THE survey PACKAGE TO ESTIMATE THE UNDERLYING LINEAR
# REGRESSION, BUT DOES NOT PROVIDE ANY GOODNESS OF FIT STATISTICS FROM THAT
# REGRESSION MODEL; TO OBTAIN THOSE STATISTICS, WE RE-ESTIMATE THE svyglm
# MODEL FOR EACH SUBSET JUST AS cj IN cregg DOES

# FIRST AMONG TASKS SITUATED IN A SCENARIO WHERE THE HIRING ORGANIZATION IS ALIGNED
# WITH THE MINORITY PARTY IN GOVERNMENT

survey_design <- svydesign(ids = ~ResponseId, weights = ~1, data = hiring_data_alignment[which(hiring_data_alignment$org_maj_min=="minority")])
reg_model <- svyglm(choice ~ app_gender + app_community + app_race + app_bilingual + 
                      app_lobby_length*app_lobby_policy_match + app_org_ideo_match + app_prev_pol_exp_type,
                    design = survey_design)

# THE RESULTING svyglm OBJECT INCLUDES AN AIC ATTRIBUTE
reg_model$aic

# THEN AMONG TASKS SITUATED IN A SCENARIO WHERE THE HIRING ORGANIZATION IS ALIGNED
# WITH THE MAJORITY PARTY IN GOVERNMENT

survey_design <- svydesign(ids = ~ResponseId, weights = ~1, data = hiring_data_alignment[which(hiring_data_alignment$org_maj_min=="majority")])
reg_model <- svyglm(choice ~ app_gender + app_community + app_race + app_bilingual + 
                      app_lobby_length*app_lobby_policy_match + app_org_ideo_match + app_prev_pol_exp_type,
                    design = survey_design)

# THE RESULTING svyglm OBJECT INCLUDES AN AIC ATTRIBUTE
reg_model$aic

################################################################################

# PAGE APPENDIX 37--TABLE A10

# SUBSET TO ONLY DEMOCRATS, REPUBLICANS, OR INDEPENDENTS ASKED TO IMAGINE THEY
# WORK FOR A LIBERAL, CONSERVATIVE, OR BIPARTISAN ORGANIZATION, RESPECTIVELY

hiring_data_likeminded <- hiring_data[which((hiring_data$pid=="Strong Democrat" & hiring_data$org_ideo=="liberal") |
                                              (hiring_data$pid=="Not a very strong Democrat" & hiring_data$org_ideo=="liberal") |
                                              (hiring_data$pid=="Lean Democrat" & hiring_data$org_ideo=="liberal") |
                                              (hiring_data$pid=="Independent" & hiring_data$org_ideo=="bipartisan") |
                                              (hiring_data$pid=="Lean Republican" & hiring_data$org_ideo=="conservative") |
                                              (hiring_data$pid=="Not a very strong Republican" & hiring_data$org_ideo=="conservative") |
                                              (hiring_data$pid=="Strong Republican" & hiring_data$org_ideo=="conservative")
),]

# AMCES FOR ATTRIBUTE-LEVELS
results <- amce(data = hiring_data_likeminded, choice ~ app_gender + app_community + app_race + app_bilingual + 
                  app_lobby_length*app_lobby_policy_match + app_org_ideo_match*app_prev_pol_exp_type,
                id = ~ ResponseId, alpha = 0.05/27)
# hiring_data_likeminded FORMATTED FOR INPUT TO TABLE
cbind(as.character(results$feature), as.character(results$level), round(results$estimate, 2), round(results$std.error,2),
      round(results$lower, 2), round(results$upper,2))
# NUMBER OF RESPONDENTS
length(unique(hiring_data_likeminded$ResponseId[which(!is.na(hiring_data_likeminded$choice))]))
# NUMBER OF PROFILE EVALUATIONS
sum(!is.na(hiring_data_likeminded$choice))

################################################################################

# PAGE APPENDIX 39--TABLE A11

# AMCES FOR ATTRIBUTE-LEVELS BY RESPONDENT PID
results_pid <- cj(data = hiring_data, choice ~ app_gender + app_community + app_race + app_bilingual + 
                app_lobby_length*app_lobby_policy_match + app_org_ideo_match*app_prev_pol_exp_type,
              id = ~ ResponseId, alpha = 0.05/27, estimate = "amce", by = ~pid3)
# results_pid FOR DEMOCRATS FORMATTED FOR TABULAR PRESENTATION
cbind(as.character(results_pid$feature), as.character(results_pid$level), round(results_pid$estimate, 2), round(results_pid$std.error,2),
      round(results_pid$lower, 2), round(results_pid$upper,2))[1:26,]
# results_pid FOR INDEPENDENTS FORMATTED FOR TABULAR PRESENTATION
cbind(as.character(results_pid$feature), as.character(results_pid$level), round(results_pid$estimate, 2), round(results_pid$std.error,2),
      round(results_pid$lower, 2), round(results_pid$upper,2))[27:52,]
# results_pid FOR REPUBLICANS FORMATTED FOR TABULAR PRESENTATION
cbind(as.character(results_pid$feature), as.character(results_pid$level), round(results_pid$estimate, 2), round(results_pid$std.error,2),
      round(results_pid$lower, 2), round(results_pid$upper,2))[53:78,]

# NUMBER OF RESPONDENTS
length(unique(hiring_data$ResponseId[which(!is.na(hiring_data$choice) &
                                             !is.na(hiring_data$pid3))]))
# NUMBER OF PROFILE EVALUATIONS
sum(!is.na(hiring_data$choice) & !is.na(hiring_data$pid3))

# NUMBER OF DEMOCRATIC RESPONDENTS
length(unique(hiring_data$ResponseId[which(!is.na(hiring_data$choice) &
                                             hiring_data$pid3=="Democrat")]))
# NUMBER OF DEMOCRATIC PROFILE EVALUATIONS
sum(!is.na(hiring_data$choice) & hiring_data$pid3=="Democrat", na.rm = TRUE)

# NUMBER OF INDEPENDENT RESPONDENTS
length(unique(hiring_data$ResponseId[which(!is.na(hiring_data$choice) &
                                             hiring_data$pid3=="Independent")]))
# NUMBER OF INDEPENDENT PROFILE EVALUATIONS
sum(!is.na(hiring_data$choice) & hiring_data$pid3=="Independent", na.rm = TRUE)

# NUMBER OF REPUBLICAN RESPONDENTS
length(unique(hiring_data$ResponseId[which(!is.na(hiring_data$choice) &
                                             hiring_data$pid3=="Republican")]))
# NUMBER OF REPUBLICAN PROFILE EVALUATIONS
sum(!is.na(hiring_data$choice) & hiring_data$pid3=="Republican", na.rm = TRUE)

# cj FROM cregg USES THE survey PACKAGE TO ESTIMATE THE UNDERLYING LINEAR
# REGRESSION, BUT DOES NOT PROVIDE ANY GOODNESS OF FIT STATISTICS FROM THAT
# REGRESSION MODEL; TO OBTAIN THOSE STATISTICS, WE RE-ESTIMATE THE svyglm
# MODEL FOR EACH SUBSET JUST AS cj IN cregg DOES

# FIRST AMONG DEMOCRATS

survey_design <- svydesign(ids = ~ResponseId, weights = ~1, data = hiring_data[which(hiring_data$pid3=="Democrat")])
reg_model <- svyglm(choice ~ app_gender + app_community + app_race + app_bilingual + 
                      app_lobby_length*app_lobby_policy_match + app_org_ideo_match + app_prev_pol_exp_type,
                    design = survey_design)

# THE RESULTING svyglm OBJECT INCLUDES AN AIC ATTRIBUTE
reg_model$aic

# FIRST AMONG INDEPENDENTS

survey_design <- svydesign(ids = ~ResponseId, weights = ~1, data = hiring_data[which(hiring_data$pid3=="Independent")])
reg_model <- svyglm(choice ~ app_gender + app_community + app_race + app_bilingual + 
                      app_lobby_length*app_lobby_policy_match + app_org_ideo_match + app_prev_pol_exp_type,
                    design = survey_design)

# THE RESULTING svyglm OBJECT INCLUDES AN AIC ATTRIBUTE
reg_model$aic

# FIRST AMONG REPUBLICANS

survey_design <- svydesign(ids = ~ResponseId, weights = ~1, data = hiring_data[which(hiring_data$pid3=="Republican")])
reg_model <- svyglm(choice ~ app_gender + app_community + app_race + app_bilingual + 
                      app_lobby_length*app_lobby_policy_match + app_org_ideo_match + app_prev_pol_exp_type,
                    design = survey_design)

# THE RESULTING svyglm OBJECT INCLUDES AN AIC ATTRIBUTE
reg_model$aic

################################################################################

# PAGE APPENDIX 41--TABLE A12

# CONDITIONAL LOGIT REQUIRES WE STRATIFY ON RESPONDENT-TASK, SO WE NEED TO CREATE
# A NEW VARIABLE INDICATING THOSE UNIQUE COMBINATIONS
hiring_data_hth$respondent_task <- paste(hiring_data_hth$ResponseId, hiring_data_hth$task, sep = "_")

# RUNNING CONDITIONAL LOGIT MODEL--NOTE THIS DOES NOT ACCOUNT FOR IMPLAUSIBLE
# COUNTERFACTUALS
cond_logit <- clogit(choice ~ app_gender + app_community + app_race + app_bilingual + 
                       app_lobby_length + app_lobby_policy_match + app_org_ideo_match + app_prev_pol_exp_type +
                       strata(respondent_task),
                     data = hiring_data_hth)
summary(cond_logit)
AIC(cond_logit)